package com.drug.mingkang.service.impl;

import com.drug.mingkang.model.ImmAppear;
import com.drug.mingkang.model.ImmPurchaseDeatis;
import com.drug.mingkang.model.ImmRepertory;
import com.drug.mingkang.mapper.ImmRepertoryMapper;
import com.drug.mingkang.model.vo.InventoryVo;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.ImmRepertoryService;
import com.drug.mingkang.utils.AuditStatus;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.stereotype.Service;
import com.drug.mingkang.mapper.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (ImmRepertory)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:14
 */
@Service("immRepertoryService")
public class ImmRepertoryServiceImpl implements ImmRepertoryService {
    @Resource
    private ImmRepertoryMapper immRepertoryMapper;

    @Resource
    private Tools tools;

    @Resource
    private ImmAppearMapper immAppearMapper;

    @Resource
    private ImmPurchaseMapper immPurchaseMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param drugId 主键
     * @return 实例对象
     */
    @Override
    public ImmRepertory queryById(String drugId,String immId) {
        return this.immRepertoryMapper.queryById(drugId,immId);
    }


    /**
     * 新增数据
     *
     * @param immRepertory 实例对象
     * @return 实例对象
     */
    @Override
    public ImmRepertory insert(ImmRepertory immRepertory) {
        this.immRepertoryMapper.insert(immRepertory);
        return immRepertory;
    }

    /**
     * 修改数据
     *
     * @param immRepertory 实例对象
     * @return 实例对象
     */
    @Override
    public int update(ImmRepertory immRepertory) {
        return this.immRepertoryMapper.update(immRepertory);
    }

    /**
     * 通过主键删除数据
     *
     * @param repertoryId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String repertoryId) {
        return this.immRepertoryMapper.deleteById(repertoryId) > 0;
    }


    /**
     * 批量新增
     * @param list
     * @return
     */
    @Override
    public int insertBatch(List<ImmRepertory> list) {
        return this.immRepertoryMapper.insertBatch(list);
    }


    /**
     * 批量修改
     * @param list
     * @return
     */
    @Override
    public int updateBatch(List<ImmRepertory> list) {
        return this.immRepertoryMapper.updateBatch(list);
    }


    /**
     * 根据公司ID查询所拥有的药品
     * @param immId
     * @return
     */
    @Override
    public Integer[] returnDrugId(String immId) {
        return this.returnDrugId(immId);
    }


    /**
     * 新增
     * @param immRepertory
     * @return
     */
    @Override
    public int insertSelective(ImmRepertory immRepertory) {
        return this.immRepertoryMapper.insertSelective(immRepertory);
    }


    /**
     * 入库
     * @param remark
     * @param purchaseDeatis
     * @param session
     */
    @Override
    public ResponseMessage updateBePutStorage(String remark, String appearTime,String purchaseDeatis, HttpSession session) throws ParseException {

        //从session中获取用户信息
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        //调用returnDrugId方法 根据公司ID查询出该公司所拥有的所有药品集合
        List drugIds = immRepertoryMapper.returnDrugId(perUsers.getCompanyId());

        //调用tools工具类中的json转list方法，把传过来的purchaseDeatis转成采购明细list
        List<ImmPurchaseDeatis> list = tools.jsonToList(purchaseDeatis, ImmPurchaseDeatis.class);

        //定义变量rows
        int rows = 0;

        //循环采购明细集合
        for (ImmPurchaseDeatis immPurchaseDeatis : list) {
            //循环药品集合
            for (int i = 0; i < drugIds.size(); i++) {
                //如果分公司有这个药品就增加数量
                if (immPurchaseDeatis.getDrugId().equals(drugIds.get(i))) {

                    //new一个分店库存对象
                    ImmRepertory immRepertory = new ImmRepertory();
                    //设置药品ID
                    immRepertory.setDrugId(immPurchaseDeatis.getDrugId());
                    //设置公司ID
                    immRepertory.setImmId(perUsers.getCompanyId());
                    //设置数量
                    immRepertory.setCount(immPurchaseDeatis.getCount());
                    //设置备注
                    immRepertory.setRemark(remark);
                    //设置采购人
                    immRepertory.setOperator(perUsers.getUserName());
                    //调用update方法修改药品库存数量
                    immRepertoryMapper.update(immRepertory);

                    //new一个库存明细对象
                    ImmAppear immAppear1 = new ImmAppear();
                    //设置仓库ID
                    immAppear1.setRepertory(immRepertory.getRepertoryId());
                    //设置药品ID
                    immAppear1.setDrugId(immPurchaseDeatis.getDrugId());
                    //new一个格式化时间类
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    //入库时间转格式
                    Date parse = simpleDateFormat.parse(appearTime);
                    //把转格式的时间设置进去
                    immAppear1.setAppearTime(parse);
                    //设置入库数量
                    immAppear1.setAppearCount(immPurchaseDeatis.getCount());
                    //设置入库状态 1为入库
                    immAppear1.setStatus("1");
                    //设置入库人
                    immAppear1.setOperator(perUsers.getUserName());
                    //设置备注
                    immAppear1.setRemark(immPurchaseDeatis.getRemark());
                    //调用insertSelective方法 增加库存明细
                    rows = immAppearMapper.insertSelective(immAppear1);
                    //结束循环
                    break;
                }
            }

            //调用queryById方法 根据药品ID和公司ID查询这个公司有没有这个药品的库存
            ImmRepertory immRepertory = immRepertoryMapper.queryById(immPurchaseDeatis.getDrugId(),perUsers.getCompanyId());
            //如果没有这个药品的库存
            if(immRepertory==null){
                //new一个分店库存对象
                ImmRepertory immRepertory1 = new ImmRepertory();
                //设置公司id
                immRepertory1.setImmId(perUsers.getCompanyId());
                //设置药品ID
                immRepertory1.setDrugId(immPurchaseDeatis.getDrugId());
                //设置入库数量
                immRepertory1.setCount(immPurchaseDeatis.getCount());
                //设置入库人
                immRepertory1.setOperator(perUsers.getUserName());
                //设置备注
                immRepertory1.setRemark(remark);
                //调用insertSelective方法 新增一个药品库存
                immRepertoryMapper.insertSelective(immRepertory1);

                //new一个库存明细对象
                ImmAppear immAppear1 = new ImmAppear();
                //设置仓库ID
                immAppear1.setRepertory(immRepertory1.getRepertoryId());
                //设置药品ID
                immAppear1.setDrugId(immPurchaseDeatis.getDrugId());
                //new一个格式化时间类
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                //入库时间转格式
                Date parse = simpleDateFormat.parse(appearTime);
                //把转格式的时间设置进去
                immAppear1.setAppearTime(parse);
                //设置入库数量
                immAppear1.setAppearCount(immPurchaseDeatis.getCount());
                //设置入库状态 1为入库
                immAppear1.setStatus("1");
                //设置入库人
                immAppear1.setOperator(perUsers.getUserName());
                //设置备注
                immAppear1.setRemark(immPurchaseDeatis.getRemark());
                //调用insertSelective方法 增加库存明细
                rows = immAppearMapper.insertSelective(immAppear1);
            }

        }
        //调用fupdate方法 根据采购订单ID修改为已入库状态
        immPurchaseMapper.fupdate(list.get(0).getPurchaseId(),String.valueOf(AuditStatus.NO_YRK.status));
        //返回
        return rows > 0 ? ResponseMessage.success("入库成功") : ResponseMessage.error("入库失败");
    }

    @Override
    public ImmRepertory updateOne(ImmAppear immApp,String immName) {
        return this.immRepertoryMapper.updateOne(immApp,immName);
    }

    /**
     * 查询所有分公司药品库存
     * @param inventoryVo 库存Vo
     * @return
     */
    @Override
    public Map<String, Object> showDrugInventoryAll(InventoryVo inventoryVo) {

        return tools.limit(this.immRepertoryMapper.queryAllByLimit(inventoryVo), this.immRepertoryMapper.count(inventoryVo));
    }

    /**
     * 根据库存id查看出入库明细
     *
     * @param drugId 药品id
     * @param companyId 公司id
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map<String,Object> findByRepertoryId(String drugId, String companyId, int page, int limit) {
        return tools.limit(this.immAppearMapper.findByRepertoryId(drugId, companyId, (page - 1) * limit, limit), this.immAppearMapper.count(drugId, companyId));
    }

    @Override
    public ImmRepertory selectDrug(String immName, String drugId) {
        return this.immRepertoryMapper.selectDrug(immName,drugId);
    }

    @Override
    public ImmRepertory queryDrug(String drugId) {
        return this.immRepertoryMapper.queryDrug(drugId);
    }
}